import type { DeptDetailResult, DeptEntityReq, DeptListByNameReq } from "@/api/types";
import request from "@/utils/http";

/** 新增系统部门 */
function create(data: DeptEntityReq): Promise<Resp<string>> {
    return request({
        url: "/sys/dept/create",
        method: "post",
        data,
    });
}

/** 删除系统部门 */
function remove(id: string): Promise<Resp<string>> {
    return request({
        url: "/sys/dept/remove",
        method: "post",
        data: { id },
    });
}

/** 更新系统部门 */
function update(data: DeptEntityReq): Promise<Resp<null>> {
    return request({
        url: "/sys/dept/update",
        method: "post",
        data,
    });
}

/** 获取系统部门详情 */
function findById(id: string): Promise<Resp<DeptDetailResult>> {
    return request({
        url: "/sys/dept/findById",
        method: "get",
        params: { id },
    });
}

/** 获取系统子部门 */
function listChildren(parentId: string): Promise<Resp<DeptDetailResult[]>> {
    return request({
        url: "/sys/dept/listChildren",
        method: "get",
        params: { parentId },
    });
}

/** 获取部门树 */
function listTree(code: string | null): Promise<Resp<DeptDetailResult[]>> {
    return request({
        url: "/sys/dept/listTree",
        method: "get",
        params: { code },
    });
}

/** 根据名称查询查询某部门下面的系统部门 */
function listByName(data: DeptListByNameReq): Promise<Resp<DeptDetailResult[]>> {
    return request({
        url: "/sys/dept/listByName",
        method: "post",
        data,
    });
}

export const deptApi = {
    create,
    findById,
    listByName,
    listChildren,
    listTree,
    remove,
    update,
};
